from typing import Optional

from pydantic import BaseModel, Field


class PaginationBase(BaseModel):
    """分页基础参数"""
    pageIndex: int = Field(1, ge=1, description="当前页")
    pageSize: int = Field(10, ge=1, description="每页数据条数")

    sort: Optional[str] = Field(None, description="排序条件")
    order: Optional[str] = Field("desc", description="排序方式")

    @property
    def offset(self) -> int:
        """计算数据库查询的offset"""
        return (self.pageIndex - 1) * self.pageSize

    @property
    def limit(self) -> int:
        """获取每页数量"""
        return self.pageSize
